package com.ytjj.qmyx.admin.controller.imController;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.im.ImRedisKeyConstant;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.eunm.im.YxImSessionStatusEnum;
import com.ytjj.qmyx.admin.model.request.im.ImMonitorListRequest;
import com.ytjj.qmyx.admin.model.request.im.ImRealTimeServiceDateListRequest;
import com.ytjj.qmyx.admin.model.request.im.ImWorkReportListRequest;
import com.ytjj.qmyx.admin.model.response.im.ImMonitorListItemResponse;
import com.ytjj.qmyx.admin.model.response.im.ImMonitorListResponse;
import com.ytjj.qmyx.admin.model.response.im.ImRealTimeServiceDateListResponse;
import com.ytjj.qmyx.admin.model.response.im.ImWorkReportListResponse;
import com.ytjj.qmyx.admin.service.imService.ImLeaveMsgService;
import com.ytjj.qmyx.admin.service.imService.ImSessionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/monitor")
@Slf4j
public class ImMonitorController {
    @Autowired
    private ImSessionInfoService imSessionInfoService;
    @Autowired
    private ImLeaveMsgService imLeaveMsgService;
    @Autowired
    private RedisService redisService;

    /**
     * 智能监控列表
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestBody ImMonitorListRequest request) {
        CommonPage<ImMonitorListItemResponse> result = imSessionInfoService.queryMonitorSessionList(request);
        Integer sessionNum = imSessionInfoService.queryMeetingSessionNum();
        long waitNum = redisService.listLength(ImRedisKeyConstant.WAIT_CONNECT_CUSTOMER_USERID);
        long leaveNum = imLeaveMsgService.count();
        int pvNum = imSessionInfoService.findToDayCount();

        ImMonitorListResponse response = new ImMonitorListResponse();
        response.setPageList(result);
        response.setSessionNum(sessionNum);//当前会话数
        response.setWaitNum(waitNum);//当前排队人数
        response.setLeaveNum(leaveNum); //留言数量
        response.setPvNum(pvNum);
        return CommonResult.success(response);
    }

    /**
     * 实时客服数据列表
     * @param request
     * @return
     */
    @PostMapping("/rtServiceData")
    public CommonResult realTimeServiceDate(@RequestBody ImRealTimeServiceDateListRequest request) {
        CommonPage<ImRealTimeServiceDateListResponse> result = imSessionInfoService.realTimeServiceData(request);
        return CommonResult.success(result);
    }

    /**
     * 客服工作报表
     * @return
     */
    @PostMapping("/workReport")
    public CommonResult workReport(@RequestBody ImWorkReportListRequest request) {
        CommonPage<ImWorkReportListResponse> result = imSessionInfoService.workReport(request);
        return CommonResult.success(result);
    }


    /**
     * 查询实时客服当前客服数据
     * @return
     */
    @GetMapping("/findServiceStatusInfo")
    public CommonResult findServiceStatusInfo() {
        try {
            Map<String, Integer> map = imSessionInfoService.findServiceStatusInfo();
            return CommonResult.success(map);
        } catch (Exception e) {
            log.error("查询实时客服当前客服数据接口报错,{}",e.getMessage());
            return  CommonResult.failed(e.getMessage());
        }
    }



}
